package cn.source.zk.service.impl;


import cn.hutool.core.date.DateUtil;
import cn.source.common.constant.Constants;

import cn.source.common.utils.DateUtils;
import cn.source.zk.domain.dto.DbDataAnalysisQueryDTO;
import cn.source.zk.domain.entity.Channel;
import cn.source.zk.mapper.DbDataAnalysisMapper;
import cn.source.zk.service.IDbAnalysisDataService;
import cn.source.zk.service.IDbFieldInfoService;
import cn.source.zk.service.IZkChannelService;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.SeriesType;
import com.github.abel533.echarts.code.Tool;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Line;
import com.github.abel533.echarts.series.Series;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.*;
import java.util.List;


@Service
@Slf4j
public class DbDataAnalysisServiceImpl implements IDbAnalysisDataService {

    @Autowired
    private DbDataAnalysisMapper dataAnalysisMapper;

    @Autowired
    private IDbFieldInfoService fieldInfoService;

    @Autowired
    private IZkChannelService channelService;

    /**
     * 折线图数据
     *
     * @param dto
     */
    @Override
    public Map<String, Object> flowLine(DbDataAnalysisQueryDTO dto) {
        // 拼接表表名   db_uhraaug db_uhrbaug db_uhrcaug
        String tableName = null;


        // HashMap<String, Object> result = new HashMap<>();
        Map<String, Object> result = new LinkedHashMap<>();
        List<String> timeList = new ArrayList<>();
        // 定义了[a,b,c]常量，组合数据库表，db_uhraaug,
        for (String fieldType : Constants.FIELD_TYPE) {
            timeList.clear();
            if (dto.getGroupMethod().equals("all")) {
                tableName = Constants.FIELD_PREFIX_DB + dto.getDataType() + fieldType;
            } else {
                tableName = Constants.FIELD_PREFIX_DB + dto.getDataType() + fieldType + dto.getGroupMethod();
            }
            // 传过来的MonitoringItem是Uat，需要将a分别替换成b,c;
            dto.setTableName(tableName);
            StringBuilder sb = new StringBuilder(dto.getMonitoringItem());
            if (sb.length() > 1) {
                // 替换第二个字符
                sb.setCharAt(1, fieldType.charAt(0));
            }
            dto.setMonitoringItem(sb.toString());
            // 执行查询，查询list；
            List<Map<String, Object>> list = dataAnalysisMapper.getFlowLine(dto);
            // 如果数据为空，返回空
//            if (list.isEmpty()) {
//                return null;
//            }
            // 将查询出来的数据放到valuesList列表中；
            List<Object> valuesList = new ArrayList<>();

            // 这里的map是放数据库查询出来的字段和值的键值对；将值提取到valuesList中，将一样的键提取出来作为键放到组成新的map；
            for (Map<String, Object> map : list) {
                valuesList.add(map.get(dto.getMonitoringItem()));
                LocalDateTime timedate = (LocalDateTime) map.get("timedate");
                timeList.add(DateUtil.format(timedate, "yyyy-MM-dd HH:mm:ss"));
            }
            // 根据字段和表名获取到注释作为map的k  ey
            String key = fieldInfoService.getCommentByField(dto.getTableName(), dto.getMonitoringItem());

            result.put(fieldType, valuesList);
        }

        result.put("timeDate",timeList);

        return result;
    }

    /**
     * 获取通道列表
     *
     * @param dto
     * @return
     */
    public List<Channel> getChannelList(DbDataAnalysisQueryDTO dto) {
        // 这里也是根据用户 db_uhra、db_uhrb、db_uhrc 会有这几种情况，并且三个表中的通道信息都一样，这里就只查询db_uhra表中的通道信息；
        String tableName = Constants.FIELD_PREFIX_DB + dto.getDataType() + 'a';
        dto.setTableName(tableName);
        List<String> tdGuidList = dataAnalysisMapper.getChannelList(dto);

        //根据tdGuid查询通道
        return channelService.getByTdGuids(tdGuidList);
    }

    @Override
    public List<Map<String, Object>> getFlowDatabase(DbDataAnalysisQueryDTO dto,String columns) {
        List<Map<String, Object>> database = dataAnalysisMapper.getFlowDatabase(dto,columns);
        return database;
    }
}
